package com.aliwo.service.impl;

import com.aliwo.config.WeChatConfig;
import com.aliwo.domain.User;
import com.aliwo.mapper.UserMapper;
import com.aliwo.service.UserService;
import com.aliwo.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

/**
 * .
 *
 * @author Xu Yayuan
 * @date 2021/11/3
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private WeChatConfig weChatConfig;

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public User findUserByOpenid(String openid) {
        return userMapper.findUserByOpenid(openid);
    }

    @Override
    public User saveWeChatUser(String code) {
        String accessTokenUrl = String.format(WeChatConfig.getOpenAccessTokenUrl(), weChatConfig.getOpenAppid(), weChatConfig.getOpenAppsecret(), code);
        //获取access_token
        Map<String, Object> baseMap = HttpUtils.doGet(accessTokenUrl, 5000);
        if (CollectionUtils.isEmpty(baseMap)) {
            return null;
        }
        String accessToken = (String) baseMap.get("access_token");
        String openId = (String) baseMap.get("openid");

        User dbUser = userMapper.findUserByOpenid(openId);
        if (null != dbUser) { // 一：更新用户，二：直接返回
            return dbUser;
        }

        //获取用户基本信息
        String userInfoUrl = String.format(WeChatConfig.getOpenUserInfoUrl(), accessToken, openId);
        //获取access_token
        Map<String, Object> baseUserMap = HttpUtils.doGet(userInfoUrl, 5000);
        Double sexTemp  = (Double) baseUserMap.get("sex");
        int sex = sexTemp.intValue();
        String province = (String) baseUserMap.get("province");
        String city = (String) baseUserMap.get("city");
        String country = (String) baseUserMap.get("country");
        String headimgurl = (String) baseUserMap.get("headimgurl");
        StringBuilder sb = new StringBuilder(country).append("||").append(province).append("||").append(city);
        String finalAddress = sb.toString();
        if (CollectionUtils.isEmpty(baseUserMap)) {
            return null;
        }
        String nickname = (String) baseUserMap.get("nickname");
        try {
            //解决乱码
            nickname = new String(nickname.getBytes("ISO-8859-1"), "UTF-8");
            finalAddress = new String(finalAddress.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        User user = new User();
        user.setName(nickname);
        user.setHeadImg(headimgurl);
        user.setCity(finalAddress);
        user.setOpenid(openId);
        user.setSex(sex);
        user.setCreateTime((Timestamp) new Date());
        userMapper.save(user);
        return user;
    }
}
